// pages/personal/statistics/statistics.js
import config from '../../../utils/config.js';
import utils from '../../../utils/util.js';
let Charts = require('../../../utils/wxcharts-min.js');
import {
  statisticsModel
} from '../../../models/statistics';
const statistics = new statisticsModel();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    config: config,
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '数据统计',
      //  导航栏 中间的标题
    },
    
    completeMoney: 0, // 已完成金额
    payedMoney: 0, // 已结账金额
    notPayMoney: 0, // 待结账金额
    sendOrderCount: 0, // 已配送订单
    notSendOrderCount: 0, // 待配送订单
    notConfirmOrderCount: 0, // 待确认订单
    otherOrderCount: 0, // 其他
    canvasTemImg1: '',
    canvasTemImg2: '',
    canvasTemImg3: '',
    statisticsData: '',

    show1: false,
    show2: false,
    date: '',//默认起始时间  
    date2: '',//默认结束时间 
    minDate1: 0,
    maxDate: 0,
    maxDate1: 0,
    currentDate: 0, 
    currentDate1:0, 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.onReset()
  },
  // 开始时间  
  onDeliveryTime(e) {
    this.setData({
      show1: true
    })
  },
  onConfirm0(event) {
    var currentDate = event.detail
    if(event.detail>this.data.currentDate1){
      currentDate = this.data.currentDate1
    }
    var date = utils.formatTimeTwo(currentDate,'Y-M-D')
    this.setData({
      currentDate: currentDate,
      date: date,
      show1: false
    });
  },
  onCancel0(){
    this.setData({
      show1: false
    })
  },
  // 结束时间
  onArriveTime(e) {
    this.setData({
      show2: true
    })
  },
  onConfirm1(event) {
    var currentDate1 = event.detail
    if(event.detail<this.data.currentDate){
      currentDate1 = this.data.currentDate
    }
    var date2 = utils.formatTimeTwo(currentDate1,'Y-M-D')
    this.setData({
      currentDate1: currentDate1,
      date2: date2,
      show2: false,
    });
  },
  onCancel1(){
    this.setData({
      show2: false
    })
  },
  onReset(){
    var nowDate = new Date()
    var year = nowDate.getFullYear()
    var mouth = nowDate.getMonth()+1
    var day = nowDate.getDate()
    var defaultDate = year+'-'+utils.formatNumber(mouth)+'-01'
    var date2 = year+'-'+utils.formatNumber(mouth)+'-'+utils.formatNumber(day)
    this.setData({
      date: defaultDate,
      date2: date2,
      maxDate: new Date().getTime(),
      currentDate: new Date(year, mouth-1, 1).getTime(), 
      // minDate1: new Date(year, mouth-1, 1).getTime(), 
      maxDate1: new Date().getTime(),
      currentDate1: new Date().getTime(), 
    })
    this.onConfirm()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },
  // 数据统计
  getStatistics(params){
    statistics.statisticsList(params, res => {
      this.setData({
        statisticsData: res.data
      })
      this.cartFun1(res.data) // 账单金额图表
      this.cartFun2(res.data) // 订单配送图表
      this.cartFun3(res.data) // 待确认订单图表
    })
  },
  // 账单金额图表
  cartFun1(data){
    var sum = data.supplierOrderBillCountVo.completeMoney + data.supplierOrderBillCountVo.payedMoney + data.supplierOrderBillCountVo.notPayMoney
    if(sum != 0){
      var completeMoney = (data.supplierOrderBillCountVo.completeMoney/sum*100).toFixed(0)
      var payedMoney = (data.supplierOrderBillCountVo.payedMoney/sum*100).toFixed(0)
      var notPayMoney = 100-completeMoney-payedMoney
      this.setData({
        completeMoney,
        payedMoney,
        notPayMoney
      })
      var chart = new Charts({
        animation: true,
        canvasId: 'canvas1',
        type: 'ring',
        extra: {
          ringWidth: 30,//圆环的宽度
        },
        series: [{
          name: '已完成金额',
          data: data.supplierOrderBillCountVo.completeMoney,
          color: '#3FDA93',
          stroke: false
        }, {
          name: '已结账金额',
          data: data.supplierOrderBillCountVo.payedMoney,
          color: '#2FBEE4',
          stroke: false
        }, {
          name: '待结账金额',
          data: data.supplierOrderBillCountVo.notPayMoney,
          color: '#F18C58',
          stroke: false
        }],
        disablePieStroke: true,
        width: 170,
        height: 170,
        dataLabel: false,
        legend: false,
        padding: 0,
        background: '#ffffff'
      });
      var _this = this
      chart.addEventListener('renderComplete', () => {
        _this.draw1()
      });

    }
  },
  // 账单金额图表表生成图片
  draw1(){
    var _this = this
    wx.canvasToTempFilePath({
      canvasId:'canvas1',
      quality: 1,
      success: res => {
        _this.setData({
          canvasTemImg1: res.tempFilePath
        })
      },
      fail: res => {
        console.log(res)
      }
    },this)

  },
  // 订单配送图表
  cartFun2(data){
    var sum = data.supplierOrderCountVo.sendOrderCount + data.supplierOrderCountVo.notSendOrderCount
    if(sum != 0){
      var sendOrderCount = (data.supplierOrderCountVo.sendOrderCount/sum*100).toFixed(0)
      var notSendOrderCount = 100-sendOrderCount
      this.setData({
        sendOrderCount,
        notSendOrderCount
      })
      var chart = new Charts({
        animation: true,
        canvasId: 'canvas2',
        type: 'ring',
        extra: {
          ringWidth: 30,//圆环的宽度
        },
        series: [{
          name: '已配送订单',
          data: data.supplierOrderCountVo.sendOrderCount,
          color: '#2FBEE4',
          stroke: false
        }, {
          name: '待配送订单',
          data: data.supplierOrderCountVo.notSendOrderCount,
          color: '#F18C58',
          stroke: false
        }],
        disablePieStroke: true,
        width: 170,
        height: 170,
        dataLabel: false,
        legend: false,
        padding: 0,
        background: '#ffffff'
      });
      var _this = this
      chart.addEventListener('renderComplete', () => {
        _this.draw2()
      });

    }
  },
  // 订单配送图表表生成图片
  draw2(){
    var _this = this
    wx.canvasToTempFilePath({
      canvasId:'canvas2',
      quality: 1,
      success: res => {
        _this.setData({
          canvasTemImg2: res.tempFilePath
        })
      },
      fail: res => {
        console.log(res)
      }
    },this)

  },
  // 待确认订单图表
  cartFun3(data){
    var sum = data.supplierOrderCountVo.notConfirmOrderCount + data.supplierOrderCountVo.otherOrderCount
    if(sum != 0){
      var notConfirmOrderCount = (data.supplierOrderCountVo.notConfirmOrderCount/sum*100).toFixed(0)
      var otherOrderCount = 100-notConfirmOrderCount
      this.setData({
        notConfirmOrderCount,
        otherOrderCount
      })
      var chart = new Charts({
        animation: true,
        canvasId: 'canvas3',
        type: 'ring',
        extra: {
          ringWidth: 30,//圆环的宽度
        },
        series: [{
          name: '待确认订单',
          data: data.supplierOrderCountVo.notConfirmOrderCount,
          color: '#2FBEE4',
          stroke: false
        }, {
          name: '其他',
          data: data.supplierOrderCountVo.otherOrderCount,
          color: '#F18C58',
          stroke: false
        }],
        disablePieStroke: true,
        width: 170,
        height: 170,
        dataLabel: false,
        legend: false,
        padding: 0,
        background: '#ffffff'
      });
      var _this = this
      chart.addEventListener('renderComplete', () => {
        _this.draw3()
      });

    }
  },
  // 待确认订单图表表生成图片
  draw3(){
    var _this = this
    wx.canvasToTempFilePath({
      canvasId:'canvas3',
      quality: 1,
      success: res => {
        _this.setData({
          canvasTemImg3: res.tempFilePath
        })
      },
      fail: res => {
        console.log(res)
      }
    },this)

  },

  // 查询
  onConfirm: utils.throttle(function (e) {
    this.getStatistics({
      startTime: this.data.date,
      endTime: this.data.date2
    })
  }),

  // 账单列表
  onBill: utils.throttle(function (e) {
    wx.navigateTo({
      url: '/pages/personal/bill/bill',
    })
  }),

  // 订单列表
  onOrder: utils.throttle(function (e) {
    wx.switchTab({
      url: '/pages/order/order',
    })
  }),

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})